--主用户数据
module("DynamicData.MainUserData", package.seeall)

local DynamicDataBase = require("DynamicData.DynamicDataBase").DynamicDataBase
local UIUtility = require("UI.Utility")
MainUserData = DynamicDataBase:new()

MainUserData.uid = 0
MainUserData.diamond = 0 --钻石
MainUserData.storage = 0 --库存
MainUserData.nickname = "unknow" --昵称
MainUserData.headimg = nil
MainUserData.sex = 1 --性别
MainUserData.BattleRecord = {} --战斗记录
MainUserData.BattleRecordDic = {} --战斗记录查询字典，传入index的字符串，得到对应的GameRecord
MainUserData.ProxyRecord = {} --战斗记录
MainUserData.ProxyRoom = {}
MainUserData.JoinClub = {}
MainUserData.MyClub = {}
MainUserData.CanCreateClub = false --能否创建俱乐部
MainUserData.noticeInfo = nil --地区公告信息
MainUserData.MyRank = - 1
MainUserData.RankPlayers = {}
MainUserData.content = "暂未设置"
MainUserData.puid = "0"
------新增
MainUserData.userId = 0 ---玩家ID
MainUserData.headimgurl = false 

--新增 181228 start
MainUserData.currentclubgold = 0
MainUserData.onclubfrist = true
----玩家身份，取消多俱乐部后用到
MainUserData.userRole = 2  --0群主，1管理 ，2群员
--181228 end

--新增 未读消息数目
MainUserData.newMsgNum = 0


function MainUserData:LoginUserData(loginSm)
	self.nickname = loginSm.nickname
	self.headimg = loginSm.headimg
	self.JoinClub = loginSm.join
	self.MyClub = loginSm.my
	self.CanCreateClub = loginSm.createJ
	self.noticeInfo = loginSm.notice
	self.content = loginSm.content
	self.sex = loginSm.sex
	self.puid = loginSm.puid
	self.diamond = loginSm.cards
	self.userId = loginSm.userId
	self.newMsgNum = loginSm.msgNum
	
	self.headimgurl = loginSm.headimg
	self.currentclubgold = loginSm.gold
	self.userRole = loginSm.userRole
end

function MainUserData:UpdateRankInfo(rankSm)
	self.MyRank = rankSm.rankIndex
	self.RankPlayers = rankSm.entry
end


--更新玩家设置的信息内容
function MainUserData:UpdateUserSetInfo(UserInfoSetSm)
	self.content = UserInfoSetSm.content
end

--更新用户数据
function MainUserData:UpdateUser(PlayerCast)
	self.diamond = PlayerCast.diamond
	self.storage = PlayerCast.storage
	Debug.log("cun:" .. PlayerCast.storage)
	Event.Brocast(EventDefine.OnMainUserDataChange, self)
	Debug.log("玩家数据更新")
end

--更新用户名称
function MainUserData:UpdateUserName(usrname)
	self.nickname = usrname
	
end

--更新战斗记录数据
function MainUserData:UpdateBattleRecord(record)
	self.BattleRecord = record
	self.BattleRecordDic = {}
	for k, v in ipairs(record) do
		local index = tostring(v.index)
		self.BattleRecordDic[index] = v
	end
	Event.Brocast(EventDefine.OnBattleRecordChange, self.BattleRecord)
	Debug.log("战斗记录数据更新")
end

function MainUserData:UpdateProxyRecord(record)
	self.ProxyRecord = record
	Event.Brocast(EventDefine.OnProxyRecordChange, self.ProxyRecord)
	Debug.log("proxy战斗记录数据更新")
end

function MainUserData:UpdateProxyRoom(rooms)
	self.ProxyRoom = rooms
	Event.Brocast(EventDefine.OnProxyRoomChange, self.ProxyRoom)
	Debug.log("proxy房间数据更新")
end

function MainUserData:AddProxyRoom(rooms)
	if rooms ~= nil and table.getn(rooms) > 0 then
		for i = 1, #rooms do
			table.insert(self.ProxyRoom, rooms[i])
		end
		Event.Brocast(EventDefine.OnProxyRoomChange, self.ProxyRoom)
		Debug.log("proxy增加房间数据更新")
	end
end

function MainUserData:DelProxyRoom(roomIds)
	if roomIds ~= nil and table.getn(roomIds) > 0 then
		for i = 1, #roomIds do
			for j = 1, #self.ProxyRoom do
				if roomIds[i] == self.ProxyRoom[j].roomId then
					table.remove(self.ProxyRoom, j)
					Debug.log("remove:~~~~~~~~~~~~~~~~~~~~" .. j)
					break
				end
			end
		end
		Event.Brocast(EventDefine.OnProxyRoomChange, self.ProxyRoom)
		Debug.log("proxy删除房间数据更新")
	end
end

function MainUserData:ChgProxyRoom(rooms)
	if rooms ~= nil and table.getn(rooms) > 0 then
		for i = 1, #rooms do
			for j = 1, #self.ProxyRoom do
				if rooms[i].roomId == self.ProxyRoom[j].roomId then
					self.ProxyRoom[j] = rooms[i]
					break
				end
			end
		end
		Event.Brocast(EventDefine.OnProxyRoomChange, self.ProxyRoom)
		Debug.log("proxy更新房间数据更新")
	end
end

--加俱乐部广播
function MainUserData:AddClubCast(AddJulebuCast)
	if AddJulebuCast.type == 2 then
		table.insert(self.MyClub, AddJulebuCast.my)
	elseif AddJulebuCast.type == 1 then
		table.insert(self.JoinClub, AddJulebuCast.my)
	end
	Event.Brocast(EventDefine.OnAddClub)
end

function MainUserData:UpdateClubCast(UpdateJulebuCast)
	if UpdateJulebuCast.type == 1 then --避免重复加入到JoinClub列表
		local isInClubList = false --加入别人俱乐部是否需要审批时要做判断 （需要审核时 会先走加俱乐部广播同意后再走更新俱乐部广播 不需要审核时 直接走更新广播 ）
		for i = 1, #self.JoinClub do
			if self.JoinClub[i].id == UpdateJulebuCast.my.id then
				self.JoinClub[i].state = 2
				isInClubList = true
				break
			end
		end
		if isInClubList == false then
			table.insert(self.JoinClub, UpdateJulebuCast.my)
			--刷新ClubMainWin的排行列表 申请加入按钮变为加入按钮 +++
			local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
			local ClubMainWin = UIWinMgr:GetWindow("ClubMainWin")
			--当从俱乐部排行榜中点击申请时 一定会寻找到相应的俱乐部  当输入数字加入俱乐部时有可能在排行榜中找不到
			if ClubMainWin.rankObjects and #ClubMainWin.rankObjects > 0 then
				for i = 1, #ClubMainWin.rankObjects do
					local id = UIUtility.FindContorl("UILabel", "m_Id", ClubMainWin.rankObjects[i])
					Debug.log("寻找寻找寻找寻找寻找寻找+++++" .. UpdateJulebuCast.my.id)
					Debug.log(id.text .. "   xxxxxxxxxxxxxxxxxx")
					if UpdateJulebuCast.my.displayId == tonumber(id.text) then
						local JoinBtn = UIUtility.GetChildTransform("JoinBtn", ClubMainWin.rankObjects[i], true)
						local JoinedSp = UIUtility.GetChildTransform("JoinedSp", ClubMainWin.rankObjects[i], true)
						JoinedSp.gameObject:SetActive(true)
						JoinBtn.gameObject:SetActive(false)
						break
					end
				end
			end
		end
	elseif UpdateJulebuCast.type == 2 then
		if UpdateJulebuCast.my.state == 3 then
			for i = 1, #self.MyClub do
				if self.MyClub[i].id == UpdateJulebuCast.my.id then
					self.MyClub[i] = UpdateJulebuCast.my
					break
				end
			end
		elseif UpdateJulebuCast.my.state == 2 then
			for i = 1, #self.JoinClub do
				if self.JoinClub[i].id == UpdateJulebuCast.my.id then
					self.JoinClub[i] = UpdateJulebuCast.my
					break
				end
			end
		end
	elseif UpdateJulebuCast.type == 3 then
		for i = 1, #self.JoinClub do
			if self.JoinClub[i].id == UpdateJulebuCast.my.id then
				self.JoinClub[i] = UpdateJulebuCast.my
				break
			end
		end
		for i = 1, #self.MyClub do
			if self.MyClub[i].id == UpdateJulebuCast.my.id then
				self.MyClub[i] = UpdateJulebuCast.my
				break
			end
		end
	end
	Debug.log("更新俱乐部")
	Event.Brocast(EventDefine.OnUpClub)
end

--删除俱乐部数据
function MainUserData:DelMyClub(DelJulebuCast)
	if DelJulebuCast.type == 3 then
		local a = #MainUserData.MyClub
		local b = #MainUserData.JoinClub
		for i = a, 1, - 1 do
			if MainUserData.MyClub[i].id == DelJulebuCast.id then
				table.remove(self.MyClub, i)
				break
			end
		end
		for i = b, 1, - 1 do
			if MainUserData.JoinClub[i].id == DelJulebuCast.id then
				table.remove(self.JoinClub, i)
				local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
				local ClubModule = require("Module.ClubModule").ClubModule
				if ClubModule.isInGame == true then
					local ClubLoobyWin = require("UI.Windows.ClubLoobyWin").ClubLoobyWin
					if ClubModule.isInClubLobby == true and DelJulebuCast.id == ClubLoobyWin.id then
						UIWinMgr:CloseWindow("ClubLoobyWin")
						--欢乐场界面优化~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
						--  UIWinMgr:OpenWindow("ClubMainWin")
						UIWinMgr:OpenWindow("HuanLeChangWin")
						--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
					end
					UIWinMgr:OpenNotice("您入驻的俱乐部（俱乐部ID：" .. DelJulebuCast.id .. "）已经被解散")
				else
				end
				break
			end
		end
	else
		local a = #MainUserData.JoinClub
		for i = a, 1, - 1 do
			if MainUserData.JoinClub[i].id == DelJulebuCast.id then
				table.remove(self.JoinClub, i)
				break
			end
		end
		if DelJulebuCast.type == 2 then
			local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
			local ClubModule = require("Module.ClubModule").ClubModule
			if ClubModule.isInGame == true then
				local ClubLoobyWin = require("UI.Windows.ClubLoobyWin").ClubLoobyWin
				if ClubModule.isInClubLobby == true and DelJulebuCast.id == ClubLoobyWin.id then
					UIWinMgr:CloseWindow("ClubLoobyWin")
					--欢乐场界面优化~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
					--   UIWinMgr:OpenWindow("ClubMainWin")
                    	Debug.log("^^^^^^^^^^^^^^^^   2")
					UIWinMgr:OpenWindow("HuanLeChangWin")
					--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
				end
				UIWinMgr:OpenNotice("您已被踢出俱乐部（俱乐部ID：" .. DelJulebuCast.id .. "）")
			end
		end
	end
	Event.Brocast(EventDefine.OnDelClub)
end
